package com.example.demo.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.api.IShipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: xumf
 * @Date: 2019/12/19 14:53
 */
@Slf4j
@Service
public class DaHuaShipService implements IShipService {

    String HOST="http://10.25.1.61:9590";
    String GET_AIS_MAP_URL=HOST+"/snapshot_api";

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String getAisShipTotalToday() {
        log.info("通过大华获取用户数据接口地址---{};",GET_AIS_MAP_URL);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("api_key","ZHOUSHAN_AIS_06");
        JSONObject object=new JSONObject();
        HttpEntity<String> request = new HttpEntity<>(object.toJSONString(), headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(GET_AIS_MAP_URL, request, String.class);
        String body = postForEntity.getBody();
        JSONObject object1=JSON.parseObject(body);
        String result= object1.getString("data");
        return result;
    }

    @Override
    public String getAisShipTotalInZsArea() {
        return null;
    }

    @Override
    public String getAisShipByParam(Integer registerId, Integer teamId, Integer areaId) {
        return null;
    }

    @Override
    public String getAisShipList(double minLon, double maxLon, double minLat, double maxLat) {
        log.info("---通过大华获取用户数据接口地址---{};",GET_AIS_MAP_URL);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("api_key","ZHOUSHAN_AIS_02");
        JSONObject object=new JSONObject();
        object.put("minlon",minLon);
        object.put("maxlon",maxLon);
        object.put("minlat",minLat);
        object.put("maxlat",maxLat);
        HttpEntity<String> request = new HttpEntity<>(object.toJSONString(), headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(GET_AIS_MAP_URL, request, String.class);
        String body = postForEntity.getBody();
        return body;
    }

    @Override
    public String getAisShipMapList(double minLon, double maxLon, double minLat, double maxLat, int precision) {
        log.info("通过大华获取用户数据接口地址---{};",GET_AIS_MAP_URL);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("api_key","ZHOUSHAN_AIS_01");
        JSONObject object=new JSONObject();
        object.put("minlon",minLon);
        object.put("maxlon",maxLon);
        object.put("minlat",minLat);
        object.put("maxlat",maxLat);
        object.put("level",precision);
        HttpEntity<String> request = new HttpEntity<>(object.toJSONString(), headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(GET_AIS_MAP_URL, request, String.class);
        String body = postForEntity.getBody();
        return body;
    }

    @Override
    public String getShipInfo(Long mmsi,Integer iState) {
        log.info("通过大华获取用户数据接口地址---{};",GET_AIS_MAP_URL);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("api_key","ZHOUSHAN_AIS_04");
        JSONObject object=new JSONObject();
        object.put("mmsi",mmsi);
        HttpEntity<String> request = new HttpEntity<>(object.toJSONString(), headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(GET_AIS_MAP_URL, request, String.class);
        String body = postForEntity.getBody();
        return body;
    }
}
